package br.com.lpoint.web.tree;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.inject.Named;

import org.primefaces.event.NodeCollapseEvent;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.event.NodeUnselectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

import br.com.document.job.JobDocument;
import br.com.document.job.session.JobSession;
import br.com.document.job.session.LPointSessionFactory;
import br.com.document.manager.session.LPSession;
import br.com.document.manager.session.SessionConfig;
import br.com.lpoint.web.selectitem.SelectItemFactory;



@Named("tree")
@SessionScoped
public class DocumentTree implements Serializable{

	private static final long serialVersionUID = 1L;
	
	private List<SelectItem> jobsDisponiveis = new ArrayList<SelectItem>();
	private List<JobDocument> jobs = null;

	private TreeNode root;
	private int x= 0;

	private TreeNode selectedNode;
	private JobDocument jobDocument;
	
	private JobSession session;

	public DocumentTree() {
		try{
			System.out.println(x++);

			System.out.println("Iniciando DocumentTree...");
			root = new DefaultTreeNode("Root", null);
			TreeNode node0 = new DefaultTreeNode("Node 0", root);
			TreeNode node1 = new DefaultTreeNode("Node 1", root);
			TreeNode node2 = new DefaultTreeNode("Node 2", root);
			
			TreeNode node00 = new DefaultTreeNode("Node 0.0", node0);
			TreeNode node01 = new DefaultTreeNode("Node 0.1", node0);
			
			TreeNode node10 = new DefaultTreeNode("Node 1.0", node1);
			TreeNode node11 = new DefaultTreeNode("Node 1.1", node1);
			
			TreeNode node000 = new DefaultTreeNode("Node 0.0.0", node00);
			TreeNode node001 = new DefaultTreeNode("Node 0.0.1", node00);
			TreeNode node010 = new DefaultTreeNode("Node 0.1.0", node01);
			
			TreeNode node100 = new DefaultTreeNode("Node 1.0.0", node10);
			
			fazAlgo();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void fazAlgo(){
		SessionConfig config = new SessionConfig();
		config.addParameter("user", null);
		config.addParameter("password", null);
		config.addParameter("schema", null);
		session = (JobSession) LPointSessionFactory.getLPointSession(config);
		jobs = session.getAllJobs();
		for (JobDocument doc : jobs) {
			System.out.println("Job recuperado => " + doc.getName());
		}
		jobsDisponiveis = SelectItemFactory.createListSelectItems(jobs, "name");
	}
	
	public JobDocument getJobDocument() {
		return jobDocument;
	}

	public void setJobDocument(JobDocument jobDocument) {
		this.jobDocument = jobDocument;
	}

	public List<JobDocument> getJobs() {
		return jobs;
	}

	public void setJobs(List<JobDocument> jobs) {
		this.jobs = jobs;
	}

	public List<SelectItem> getJobsDisponiveis() {
		return jobsDisponiveis;
	}

	public void setJobsDisponiveis(List<SelectItem> jobsDisponiveis) {
		this.jobsDisponiveis = jobsDisponiveis;
	}

	public TreeNode getRoot() {
		return root;
	}

	public TreeNode getSelectedNode() {
		return selectedNode;
	}

	public void setSelectedNode(TreeNode selectedNode) {
		this.selectedNode = selectedNode;
	}

	public void onNodeExpand(NodeExpandEvent event) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Expanded", event.getTreeNode().toString());

		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public void onNodeCollapse(NodeCollapseEvent event) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Collapsed", event.getTreeNode().toString());

		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public void onNodeSelect(NodeSelectEvent event) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", event.getTreeNode().toString());

		FacesContext.getCurrentInstance().addMessage(null, message);
	}

    public void onNodeUnselect(NodeUnselectEvent event) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Unselected", event.getTreeNode().toString());

		FacesContext.getCurrentInstance().addMessage(null, message);
	}

}
